package cn.geminis.data.jpa.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.UUID;

@NamedEntityGraphs(
        {
                @NamedEntityGraph(
                        name = "Book.student",
                        attributeNodes = {
                                @NamedAttributeNode("student")
                        }
                )
        }
)
@Entity
@Getter
@Setter
public class Book {

    @Id
    @Column(length = 36)
    private String id;

    @Column(length = 256)
    private String name;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Student student;

    @PrePersist
    void createId() {
        if (!StringUtils.hasText(this.id)) {
            this.id = UUID.randomUUID().toString();
        }
    }
}
